<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/6
 * Time: 下午11:47
 */

namespace App\Admin\Controllers;

use App\Models\Company;
use App\Models\User;
use App\Models\Voucher;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Controllers\Controller;
use App\Busi\Traits\CompanyTrait;
use App\Busi\Traits\VoucherTrait;

class UserController extends Controller
{
    use ModelForm,CompanyTrait,VoucherTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增用户');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑用户');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('用户名');
            $grid->phone('电话号码');
            $obj = $this;
            $grid->uid("用户uid");
            $grid->company_name("关联企业")->display(function () use ($obj){
                $uid = $this->uid;
                return $obj->getCompanyNameByUID($uid);
            });
//            $grid->vouchers("关联的订单券")->display(function ($vouchers){
//                $vouchers = array_map(function ($voucher) {
//                    return "<a class='success' href='voucher/".$voucher['id']."' target='_blank'>{$voucher['name']}</a><br>";
//                }, $vouchers);
//
//                return join('', $vouchers);
//            });
//            $grid->amount("订单余额");
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->updated_at('更新时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(User::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');

            }
            $form->text('name','用户名');
            $form->text('phone','电话号码');
//            $form->multipleSelect('vouchers','关联订单券')->options(Voucher::all()->pluck('name', 'id'));
//            $form->text('amount', '用户余额');

            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是'])->default(1);
        });
    }
}